/**
 * Project Name:fire-industry-DM-center
 * File Name:CompanyServer.java
 * Package Name:com.firestone.web.company
 * Date:2018年4月12日下午3:05:07
 *
*/

package com.firestone.web.company;

import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.firestone.bean.dto.PageDTO;
import com.firestone.bean.dto.ReturnDTO;
import com.firestone.bean.dto.company.SearchDTO;
import com.firestone.bean.entity.elasticsearch.dm.DmMapProject;
import com.firestone.bean.entity.elasticsearch.im.DmImProject;
import com.firestone.bean.po.company.MapListPo;
import com.firestone.bean.po.company.PullPO;
import com.firestone.bean.po.company.PushPO;
import com.firestone.bean.vo.BaseListVO;
import com.firestone.bean.vo.company.MapListVO;
import com.firestone.converters.company.ConverterCompany;
import com.firestone.core.constants.ConstantKey;
import com.firestone.service.company.IProjectService;
import com.firestone.web.BaseServer;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: TODO(企业管理控制器)
 * @author CAIYJ
 * @date 2018年4月12日 下午3:05:07
 */
@Api(value = "企业管理控制器", tags = { "企业管理接口文档" })
@RestController
@RequestMapping(value = "/company/{areaId}")
public class CompanyServer extends BaseServer {

    @Autowired
    private IProjectService projectService;

    /**
     * @Description: TODO(企业仓库企业列表)
     * @param po
     * @param page
     * @return
     * @throws Exception
     * @author CAIYJ
     * @date 2018年4月13日 上午10:21:13
     * @lastModifier CAIYJ
     */
    @ApiOperation(value = "企业库列表", notes = "企业库列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaId", value = "区域ID", required = false, dataType = "String", paramType = "path") })
    @RequestMapping(value = "/mapList")
    public ReturnDTO<BaseListVO<MapListVO>> mapList(MapListPo po, PageDTO page)
            throws Exception {
        SearchDTO search = new SearchDTO();
        BeanUtils.copyProperties(po, search);
        // 企业仓库企业列表
        long time = System.currentTimeMillis();
        Page<DmMapProject> dmPage = projectService.mapList(search, page);
        System.out.println(System.currentTimeMillis() - time);
        // 查询产业地图企业表是否存在
        Page<DmImProject> imPage = projectService.imListByIds(dmPage);
        System.out.println(System.currentTimeMillis() - time);
        return ReturnDTO
                .success(ConverterCompany.convertMapListVO(dmPage, imPage));
    }

    /**
     * @Description: TODO(企业库企业详情)
     * @param id
     * @return
     * @throws Exception
     * @author CAIYJ
     * @date 2018年4月13日 上午10:26:41
     * @lastModifier CAIYJ
     */
    @ApiOperation(value = "企业库企业详情", notes = "企业库企业详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "企业ID", required = false, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "areaId", value = "区域ID", required = false, dataType = "String", paramType = "path") })
    @RequestMapping(value = "/mapInfo/{id}")
    public ReturnDTO<?> mapInfo(@PathVariable String id) throws Exception {
        return null;
    }

    /**
     * @Description: TODO(往产业地图推送企业)
     * @return
     * @throws Exception
     * @author CAIYJ
     * @date 2018年4月13日 上午10:18:11
     * @lastModifier CAIYJ
     */
    @ApiOperation(value = "往产业地图推送企业", notes = "往产业地图推送企业", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaId", value = "区域ID", required = false, dataType = "String", paramType = "path") })
    @RequestMapping(value = "/push")
    public ReturnDTO<String> push(@PathVariable Long areaId, PushPO pushPO)
            throws Exception {
        if (pushPO.getIds() != null && pushPO.getIds().length > 0) {
            SearchQuery searchQuery = new NativeSearchQueryBuilder()
                    .withQuery(QueryBuilders.idsQuery(ConstantKey.ES_TYPE)
                            .addIds(pushPO.getIds()))
                    .build();
            projectService.push(searchQuery, areaId);
        }
        return ReturnDTO.success("推送成功");
    }

    /**
     * @Description: TODO(产业地图企业下线)
     * @return
     * @throws Exception
     * @author CAIYJ
     * @date 2018年4月13日 上午10:18:43
     * @lastModifier CAIYJ
     */
    @ApiOperation(value = "产业地图企业下线", notes = "产业地图企业下线", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaId", value = "区域ID", required = false, dataType = "String", paramType = "path") })
    @RequestMapping(value = "/pull")
    public ReturnDTO<String> pull(@PathVariable Long areaId, PullPO pullPO)
            throws Exception {
        if (pullPO != null && pullPO.getIds() != null
                && pullPO.getIds().length > 0) {
            SearchQuery searchQuery = new NativeSearchQueryBuilder()
                    .withQuery(QueryBuilders.idsQuery(ConstantKey.ES_TYPE)
                            .addIds(pullPO.getIds()))
                    .build();
            projectService.pull(searchQuery, areaId);
        }
        return null;
    }

    /**
     * @Description: TODO(产业地图企业列表)
     * @return
     * @throws Exception
     * @author CAIYJ
     * @date 2018年4月13日 上午10:20:08
     * @lastModifier CAIYJ
     */
    @ApiOperation(value = "产业地图企业列表", notes = "产业地图企业列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaId", value = "区域ID", required = false, dataType = "String", paramType = "path") })
    @RequestMapping(value = "/imList")
    public ReturnDTO<?> imList() throws Exception {
        return null;
    }

}
